package cn.zxf.note.note.type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.zxf.note.common.exception.BizException;

@Component
public class NoteTypeSupport {

    @Autowired
    private NoteTypeRepository repos;
    @Autowired
    private NoteTypeDao dao;

    public NoteType validateAndGet(Integer typeId) {
        if (typeId == null)
            throw BizException.of("笔记类型 ID 为空");
        return repos.findById(typeId)
                .orElseThrow(() -> BizException.of("笔记类型不存在"));
    }

    public NoteType findOrSave(Integer userId, String name) {
        NoteType type = dao.findByName(userId, name);
        if (type != null)
            return type;
        type = new NoteType()
                .setUserId(userId)
                .setName(name)
                .setStatus(NoteTypeStatusEnum.NORMAL.code);
        repos.save(type);
        return type;
    }

}
